//
//  NQPickerView.m
//  ZLFastDoctor
//
//  Created by 李岚清 on 16/5/23.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "NQPickerView.h"
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>

@interface NQPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
/* 设置类型 *///fengexian
@property (nonatomic, strong) UILabel *typeLabel;
/* pierView */
@property (nonatomic, strong) UIPickerView *pickerView;
/* 取消按钮 */
@property (nonatomic, strong) UIButton *cancelBtn;
/* 提交按钮 */
@property (nonatomic, strong) UIButton *postBtn;
/* 分界线 */
@property (nonatomic, strong) UIView *boundLine;


/* typeName */
@property (nonatomic, copy) NSString *typeName;
/* typeUnit */
@property (nonatomic, copy) NSString *typeUnit;

@end

@implementation NQPickerView
#pragma mark - 按钮点击事件
//取消
- (void)cancelPick{
    [self removeFromSuperview];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(pickerViewDidClickBtn:result:pickerViewtypeName:)]) {
        [self.delegate pickerViewDidClickBtn:NQPickerBtnTypeCancel result:0 pickerViewtypeName:self.typeName];
    }
}

//提交
- (void)post{
    [self removeFromSuperview];
    //拿到当前被选中的数值
    NSInteger a = [self.pickerView selectedRowInComponent:0];
    NSInteger b = [self.pickerView selectedRowInComponent:1];
    NSInteger c = [self.pickerView selectedRowInComponent:2];
    //拼接字符串
    NSString *result = nil;
    if (a>0) {
        result = [NSString stringWithFormat:@"%zd%zd%zd%@",a,b,c,self.typeUnit];
    }else if (a==0 & b==0){
        result = [NSString stringWithFormat:@"%zd%@",c,self.typeUnit];
    }else{
        result = [NSString stringWithFormat:@"%zd%zd%@",b,c,self.typeUnit];
    }
    //传递选中值
    if ([self.delegate respondsToSelector:@selector(pickerViewDidClickBtn:result:pickerViewtypeName:)]) {
        [self.delegate pickerViewDidClickBtn:NQPickerBtnTypePost result:result pickerViewtypeName:self.typeName];
    }
}

#pragma mark - 构造方法
+ (instancetype)nq_pickerViewWithType:(NSString *)type unit:(NSString *)unit{
    NQPickerView *pickerView = [[self alloc] init];
    pickerView.typeName = type;
    pickerView.typeUnit = unit;
    pickerView.typeLabel.text = [NSString stringWithFormat:@"%@(%@)",type,unit];
    return pickerView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupAttrabute];
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

//设置frame
- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    //设置frame
    self.frame = CGRectMake(NQScreenW*0.1, NQScreenH*0.3, NQScreenW*0.8, 180);
}

//属性设置
- (void)setupAttrabute{
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
}

//添加子控件及约束
- (void)setupUI{
    //添加到view自身上
    [self addSubview:self.typeLabel];
    [self addSubview:self.pickerView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.postBtn];
    [self addSubview:self.boundLine];
    //设置约束
    [self.typeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).equalTo(10);
        make.centerX.equalTo(self);
    }];
    [self.pickerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.bottom);
        make.centerX.equalTo(self);
        make.height.equalTo(80);
        make.width.equalTo(180);
    }];
    [self.cancelBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pickerView.bottom);
        make.bottom.equalTo(self).equalTo(-10);
        make.width.height.equalTo(self.postBtn);
        make.left.equalTo(self);
    }];
    [self.postBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pickerView.bottom);
        make.bottom.equalTo(self).equalTo(-10);
        make.left.equalTo(self.cancelBtn.right);
        make.right.equalTo(self);
    }];
    [self.boundLine makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.postBtn).equalTo(-10);
        make.width.equalTo(1);
        make.top.equalTo(self.postBtn).equalTo(5);
        make.right.equalTo(self.postBtn.left);
    }];

}

#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
//组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    //大数组中数组的个数
    return 3;
}

//每组行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //取出第component组的数组内元素总个数
    return (component == 0 ? 3 : 10);
}

//每行返回属性字符串
-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%zd",row%[pickerView numberOfRowsInComponent:component]] attributes:@{NSForegroundColorAttributeName:NQColor(24, 188, 187)}];
    return  attrString;
}

#pragma mark - 懒加载子控件
- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] init];
        _typeLabel.text = @"身高(cm)";
        _typeLabel.textColor = NQColor(24, 188, 187);
        _typeLabel.font = [UIFont systemFontOfSize:16];
    }
    return _typeLabel;
}

- (UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_cancelBtn addTarget:self action:@selector(cancelPick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIView *)boundLine{
    if (!_boundLine) {
        _boundLine = [[UIView alloc] init];
        _boundLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _boundLine;
}

- (UIButton *)postBtn{
    if (!_postBtn) {
        _postBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_postBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_postBtn setTitleColor:NQColor(24, 188, 187) forState:UIControlStateNormal];
        _postBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_postBtn addTarget:self action:@selector(post) forControlEvents:UIControlEventTouchUpInside];
    }
    return _postBtn;
}

@end
